<template>
	<view class="wrap">
		<u-modal content="用户未注册,请点击确认注册" v-model="showRegister" @confirm="registerUser"></u-modal>
	</view>
</template>
<script>
export default {
	data() {
		return {
			showRegister: false,
			userId: undefined,
			username: undefined
		};
	},
	methods: {
		async getRegisterProfile() {
			try {
				// 获取 code，然后调用微信登录接口判断是否登录 401 则跳转到绑定页面
				const res = await uni.login({ provider: "weixin" });
				const grant_type = "mobile";
				const code = res[1].code;
				const mobile = `APP-MINI@${code}`;
				const scope = "server";
				const tokenRes = await this.$u.api.vxLogin(
					{ grant_type, code, mobile, scope }
				);

				// 登陆成功，存储相关信息
				this.$u.vuex("vuexToken", tokenRes);
				// 查询用户详细信息并储存
				const { data } = await this.$u.api.app.getUserInfo();
				this.$u.vuex("vuexUser", data.appUser);

				// 证明信息不完整
				if (data.appUser.nickname === null) {
					this.showRegister = true
					this.username = data.appUser.username
					this.userId = data.appUser.userId
				} else {
					this.$u.vuex("vuexLoginType", "toc");
					uni.reLaunch({
						url: "/pages/sys/home/indexToc",
					});
				}
			} catch (e) {
				console.error(e)
			}
		},

		registerUser() {
			var tmp = this;
			uni.getUserProfile({
				lang: 'zh_CN',
				desc: '获取用户信息',
				success: async res => {
					let model = {
						userId: tmp.userId,
						username: tmp.username,
						nickname: res.userInfo.nickName,
						avatar: res.userInfo.avatarUrl
					}
					// 更新用户信息
					await tmp.$u.api.app.updateUserInfo(model)
					const { data } = await this.$u.api.app.getUserInfo();
					this.$u.vuex("vuexUser", data.appUser);

					uni.reLaunch({
						url: "/pages/sys/home/indexToc",
					});
				},
				fail: err => {
					console.log(err, 'err')
				}
			});
		},
	}
}
</script>
